//
//  AyTabBar.m
//  xWeibo
//
//  Created by alvis mac on 8/1/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyTabBar.h"
#import "AyTabBarButton.h"
#import "AyDefine.h"
#import "UIImageExtend.h"
@interface AyTabBar()
@property (nonatomic, strong)  NSMutableArray * taBarButtons;
@property (nonatomic, weak)  UIButton * plusButton;
@property (nonatomic, weak)  AyTabBarButton * selectedButton;
@end


@implementation AyTabBar

- (NSMutableArray *)taBarButtons{
    if(!_taBarButtons)
    {
        _taBarButtons=[NSMutableArray array];
    }
    return _taBarButtons;
}


- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
    
        if (!iOS7) {
            self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"tabbar_background"]];
        }
        
        UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        plusButton.bounds = CGRectMake(0, 0, plusButton.currentBackgroundImage.size.width, plusButton.currentBackgroundImage.size.height);
        [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:plusButton];
        self.plusButton = plusButton;
    
    }
    return self;
}


- (void) plusClick{
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickedPlusButton:)]) {
        [self.delegate tabBarDidClickedPlusButton:self];
    }
}


- (void)addTabBarButtonWithItem:(UITabBarItem *)item{
    AyTabBarButton * button=[[AyTabBarButton alloc] init];
    [self addSubview:button];
    [self.taBarButtons addObject:button];
    
    button.item=item;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];

    //default select
    if (self.taBarButtons.count == 1) {
        [self buttonClick:button];
    }

    
}

- (void) buttonClick:(AyTabBarButton *) sender{
 
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) {
     [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:sender.tag];
    }
    
    self.selectedButton.selected=NO;
    sender.selected=YES;
    self.selectedButton=sender;

}

- (void)layoutSubviews{
    [super layoutSubviews];

    CGFloat h = self.frame.size.height;
    CGFloat w = self.frame.size.width;
    self.plusButton.center = CGPointMake(w * 0.5, h * 0.5);

    CGFloat buttonH = h;
    CGFloat buttonW = w / self.subviews.count;
    CGFloat buttonY = 0;
    
    for (int index = 0; index<self.taBarButtons.count; index++) {
     
        AyTabBarButton *button = self.taBarButtons[index];
        CGFloat buttonX = index * buttonW;
        if (index > 1) {
            buttonX += buttonW;
        }
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        button.tag = index;

    }
}




@end
